package com.example.demo.service.impl;
import com.example.demo.service.MailService;
import jakarta.mail.MessagingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;

//import javax.mail.internet.MimeMessage;
//import javax.mail.internet.MimeUtility;
import java.io.File;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

@Service
public class MailServiceImpl implements MailService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    JavaMailSenderImpl mailSender;

    @Value("${spring.mail.username}")
    private String from;

    @Override
    public void sendSimpleMail(String to, String subject, String verificationCode) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText("您的验证码是：" + verificationCode);
        mailSender.send(message);
        // 日志信息
        logger.info("验证码已经发送。");
    }
//
//    @Override
//    public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) {
//        MimeMessage message = mailSender.createMimeMessage();
//        MimeMessageHelper helper = null;
//
//        try{
//            helper = new MimeMessageHelper(message, true);
//            helper.setFrom(from);
//            helper.setSubject(subject);
//            helper.setTo(to);
//            helper.setText(content, true);
//
//            FileSystemResource fs = new FileSystemResource(new File(rscPath));
//            helper.addInline(rscId, fs);
//
//            mailSender.send(message);
//        }catch (MessagingException e) {
//            logger.error("发送邮件时发生异常！", e);
//        }
//    }
}
